package com.gopay.bis.ofpay.operation.transformer;

import java.math.BigDecimal;

import com.gopay.bis.ofpay.constants.OfpayConstants;
import com.gopay.bis.ofpay.operation.transformer.doc.FlowChargeAbleRes;
import com.gopay.bis.ofpay.operation.transformer.doc.MobiChargeAbleRes;
import com.gopay.bis.ofpay.operation.transformer.doc.OilCardInfoRes;
import com.gopay.common.constants.bank.tpas.TpasMwaChargeConstants;
import com.gopay.common.domain.bank.tpas.mwa.FlowMessage;
import com.gopay.common.domain.bank.tpas.mwa.MobileMessage;
import com.gopay.common.domain.bank.tpas.mwa.OilCardMessage;
import com.thoughtworks.xstream.XStream;

public class TransformerQuery {
    public MobileMessage parseMobiChargeAble(String xml) {
        MobiChargeAbleRes xmlBean = new MobiChargeAbleRes();
        XStream xstream = new XStream();
        xstream.autodetectAnnotations(true);
        xstream.processAnnotations(MobiChargeAbleRes.class);
        xmlBean = (MobiChargeAbleRes) xstream.fromXML(xml);
        
        MobileMessage res = new MobileMessage();
        if(OfpayConstants.QUERY_SUCCESS.equals(xmlBean.getRetcode())){
            res.setRetCode(TpasMwaChargeConstants.QUERY_SUCCESS);
        }else{
            res.setRetCode(TpasMwaChargeConstants.QUERY_FAIL);
            res.setRetMsg("暂不支持该交易");
        }
        res.setGoodsName(xmlBean.getCardname());
        res.setMobileBelong(xmlBean.getGame_area());
        res.setAmount(new BigDecimal(xmlBean.getInprice()));
        return res;
    }

    public FlowMessage parseFlowAbleCharge(String xml) {
        FlowChargeAbleRes xmlBean = new FlowChargeAbleRes();
        XStream xstream = new XStream();
        xstream.autodetectAnnotations(true);
        xstream.processAnnotations(FlowChargeAbleRes.class);
        xmlBean = (FlowChargeAbleRes) xstream.fromXML(xml);
        
        FlowMessage res = new FlowMessage();
        if(OfpayConstants.QUERY_SUCCESS.equals(xmlBean.getRetcode())){
            res.setRetCode(TpasMwaChargeConstants.QUERY_SUCCESS);
        }else{
            res.setRetCode(TpasMwaChargeConstants.QUERY_FAIL);
            res.setRetMsg("暂不支持该交易");
        }
        res.setGoodsName(xmlBean.getProductname());
        res.setAmount(new BigDecimal(xmlBean.getSaleprice()));
        return res;
    }

    public OilCardMessage parseOilCardInfo(String xml) {
        OilCardInfoRes xmlBean = new OilCardInfoRes();
        XStream xstream = new XStream();
        xstream.autodetectAnnotations(true);
        xstream.processAnnotations(OilCardInfoRes.class);
        xmlBean = (OilCardInfoRes) xstream.fromXML(xml);
        
        OilCardMessage res = new OilCardMessage();
        if(OfpayConstants.CHARGE_SUCCESS.equals(xmlBean.getRetcode())){
            res.setRetCode(TpasMwaChargeConstants.QUERY_SUCCESS);
        }else{
            res.setRetCode(TpasMwaChargeConstants.QUERY_FAIL);
            res.setRetMsg(xmlBean.getErr_msg());
        }
        res.setOuterCustID(xmlBean.getGame_userid());
        res.setUsername(xmlBean.getUsername());
        return res;
    }
}
